package com.service.business.fil.controller;

import cn.jiguang.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.service.business.base.service.ConfigService;
import com.service.business.fil.service.MerchantFilfoxConfigService;
import com.service.business.fil.service.SysQuotesIpfxService;
import com.service.business.framework.log.RequestLog;
import com.service.business.order.dto.IPFSConstant;
import com.service.business.support.MemCacheNoTran;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.model.MerchantFilfoxConfig;
import com.service.dao.model.SysConfig;
import com.service.dao.model.SysQuotesIpfx;
import com.service.utility.common.UUIDUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/fil")
@Api(tags = "FIL行情数据")
public class FILController extends BaseController {
	@Autowired
	private MemCacheNoTran memCache;
	@Autowired
	private SysQuotesIpfxService sysQuotesIpfxService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private MerchantFilfoxConfigService merchantFilfoxConfigService;

	@GetMapping("/info")
	@ApiOperation("FIL行情数据")
	@RequestLog("FIL行情数据")
	public BaseResponse info(HttpServletRequest request) {
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		String IPFS_CONFIG = memCache.get("ipfs_config");
		if (StringUtils.isNotEmpty(IPFS_CONFIG)) {
			Object rep = JSONObject.parse(IPFS_CONFIG);
			return successQueryOneResponse(ModelMapperUtil.map(rep, SysQuotesIpfx.class));
		} else {
			//读取数据库信息
			LambdaQueryWrapper<SysQuotesIpfx> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.orderByDesc(SysQuotesIpfx::getUpdateTime);
			queryWrapper.last("limit 1");
			SysQuotesIpfx quotesIpfx = sysQuotesIpfxService.getOne(queryWrapper);
			return successQueryOneResponse(quotesIpfx);
		}
	}

	@GetMapping("/merchant-info")
	@ApiOperation("商家FIL行情数据")
	@RequestLog("商家FIL行情数据")
	public MultiResponse<MerchantFilfoxConfig> merchantInfo(HttpServletRequest request) {
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		//获取商家节点列表配置信息
		SysConfig config = configService.getByName(ConfigConstants.MERCHANT_FIL_FOX_LIST);
		List<MerchantFilfoxConfig> list = new ArrayList<>();
		if (ObjectUtils.isEmpty(config)) {
			log.warn("暂无FilFox商家节点列表配置");
			return successQueryMultiResponse(list);
		}
		String[] merchantSymbols = config.getConfValue().split("/");
		if (merchantSymbols.length < 1) {
			log.warn("暂无FilFox商家节点列表配置");
			return successQueryMultiResponse(list);
		}
		List<String> merchantSymbolList = Arrays.asList(merchantSymbols);
		for (String merchantSymbol : merchantSymbolList) {
			String MERCHANT_IPFX_INFO_KEY = memCache.get(IPFSConstant.MERCHANT_IPFX_INFO_KEY + merchantSymbol);
			Object rep = JSONObject.parse(MERCHANT_IPFX_INFO_KEY);
			list.add(ModelMapperUtil.map(rep, MerchantFilfoxConfig.class));
		}
		//降序排序
		list.sort(Comparator.comparing(MerchantFilfoxConfig::getRankNo));
		return successQueryMultiResponse(list);
	}
}
